﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Tesseract;

namespace TesseractDemo
{
    public class SimpleEngCaptcha
    {
        private TesseractEngine te;
        public SimpleEngCaptcha()
        {
            string defaultList = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.=";
            te = new TesseractEngine(@"..\..\tessdata\", "eng");//chi_sim
            te.SetVariable("SimpleEngCaptcha", defaultList);
        }

        public string FromStr(string content)
        {
            try
            {
                byte[] byteContent = Convert.FromBase64String(content);
                return FromBytes(byteContent);
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        public string FromBytes(byte[] content)
        {
            try
            {
                MemoryStream ms = new MemoryStream(content)
                {
                    Position = 0
                };
                Bitmap bitmap = new Bitmap(ms);
                Page page = te.Process(bitmap, te.DefaultPageSegMode);
                var str = page.GetText().Trim();
                return str;
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        public string FromFile(string fileName)
        {
            try
            {
                Image image = Image.FromFile(fileName);
                Bitmap bitmap = new Bitmap(image);
                Page page = te.Process(bitmap, te.DefaultPageSegMode);
                var str = page.GetText().Trim();
                return str;
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        public string SimpleArithmetic(string content)
        {
            var result = Regex.Matches(content, "[?\\d]{1,}");
            int[] num = new[] { -1, -1, -1 };
            int j = -1;
            for (int i = 0; i < result.Count; i++)
            {
                if (!result[i].Value.Contains("?"))
                {
                    num[i] = Convert.ToInt32(result[i].Value);
                }
                else
                {
                    j = i;
                }
            }
            var ope = Regex.Match(content, "[+\\-]").Value;
            if (j == 2)
            {
                switch (ope)
                {
                    case "+":
                        return Convert.ToString(num[0] + num[1]);
                    case "-":
                        return Convert.ToString(num[0] - num[1]);
                    default:
                        break;
                }
            }
            if (j == 1)
            {
                switch (ope)
                {
                    case "+":
                        return Convert.ToString(num[2] - num[0]);
                    case "-":
                        return Convert.ToString(num[0] - num[2]);
                    default:
                        break;
                }
            }
            if (j == 0)
            {
                switch (ope)
                {
                    case "+":
                        return Convert.ToString(num[2] - num[0]);
                    case "-":
                        return Convert.ToString(num[2] + num[1]);
                    default:
                        break;
                }
            }
            return "";
        }
    }
}
